﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Venues
{
    public class VenuesRaceInfo : IHttpHandler
    {
        private Jy123.Model.Venues_Main venues;
        private Jy123.Model.Club_Main club;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;

        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            string key = "VenuesId";
            string venuesIdString = context.Request.QueryString[key];
            string strMark = context.Request.QueryString["Mark"];
            if (string.IsNullOrEmpty(strMark))
            {
                strMark = context.Request["Mark"];
            }
            string strClubId;

            int venuesId = int.Parse(venuesIdString);
            Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

            venues = BLLVenues.GetModel(venuesId);
            string[] strCId = venues.RaceClub.Split(',');
            if (strCId.Length > int.Parse(strMark))
            {
                strClubId = strCId[int.Parse(strMark)];

                user = BLLUser.GetModel(venues.FUser.Value);
                level = BLLLevel.GetModel(venues.FLevel.Value);
                if (!string.IsNullOrEmpty(strClubId))
                {
                    int clubId = int.Parse(strClubId);
                    club = BLLClub.GetModel(clubId);
                }
                else
                {
                    club = new Jy123.Model.Club_Main();
                }

                Jy123.BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
                List<Jy123.Model.Public_Props> listProps = BLLProps.GetModelList("FPType = 6");
                Dictionary<int, Jy123.Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();
                foreach (Jy123.Model.Public_Props props in listProps)
                {
                    dicProps.Add(props.ID, props);
                }

                string strResult = ReplaceRaceInfo(dicProps, context);
                context.Response.Write(strResult);
            }

        }

        private string ReplaceRaceInfo(Dictionary<int, Jy123.Model.Public_Props> dicProps, HttpContext context)
        {
            string strRaceInfo = context.Cache["venuesraceinfodiv"].ToString();
            StringBuilder sbString = new StringBuilder();
            Jy123.BLL.Club_Supporter BLLSupporter = new Jy123.BLL.Club_Supporter();
            Jy123.BLL.Public_Game BLLGame = new Jy123.BLL.Public_Game();
            string strGameWhere = string.Format("FVenues = {0} AND IsEnd = 0", venues.ID.ToString());
            List<Jy123.Model.Public_Game> listGame = BLLGame.GetModelList(strGameWhere);

            if (listGame.Count > 0)
            {
                string strFWhere = string.Format("ID IN (SELECT FFriend FROM User_Friend WHERE FUser = {0})", venues.FUser.Value.ToString());
                string[] strCId = venues.RaceClub.Split(',');
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                if (strCId.Length > 1)
                {
                    Model.Club_Main club1 = BLLClub.GetModel(int.Parse(strCId[0]));
                    Model.Club_Main club2 = BLLClub.GetModel(int.Parse(strCId[1]));
                    strFWhere = string.Format("ID IN (SELECT FFriend FROM User_Friend WHERE FUser = {0} OR  FUser = {1} OR FUser = {2})", venues.FUser.Value.ToString(), club1.FUser.Value.ToString(), club2.FUser.Value.ToString());
                }
                else
                {
                    Model.Club_Main club1 = BLLClub.GetModel(int.Parse(strCId[0]));
                    strFWhere = string.Format("ID IN (SELECT FFriend FROM User_Friend WHERE FUser = {0} OR  FUser = {1})", venues.FUser.Value.ToString(), club1.FUser.Value.ToString());
                }

                Jy123.BLL.User_Main BLLFriend = new Jy123.BLL.User_Main();
                List<Jy123.Model.User_Main> listFriend = BLLFriend.GetModelList(strFWhere);

                Dictionary<int, Jy123.Model.User_Main> dicUser = new Dictionary<int, Jy123.Model.User_Main>();
                foreach (Jy123.Model.User_Main cUser in listFriend)
                {
                    dicUser.Add(cUser.ID, cUser);
                }


                Jy123.Model.Public_Game game = listGame[0];
                string strSpWhere = string.Format("FGame = {0} AND FClub = {1}", game.ID.ToString(), club.ID.ToString());
                List<Jy123.Model.Club_Supporter> listSupporter = BLLSupporter.GetModelList(strSpWhere);
                foreach (Jy123.Model.Club_Supporter supporter in listSupporter)
                {
                    string strUl = context.Cache["venuesraceinfoul"].ToString();
                    string userName = dicUser[supporter.FUser.Value].UserName;
                    string strHelp = "加油";
                    Jy123.Model.Public_Props cProps = dicProps[supporter.FProps];
                    int value = 0;
                    if (cProps.Liveness.HasValue)
                    {
                        value = value + cProps.Liveness.Value;
                    }
                    if (cProps.Moral.HasValue)
                    {
                        value = value + cProps.Moral.Value;
                    }
                    if (cProps.PhysicalFitness.HasValue)
                    {
                        value = value + cProps.PhysicalFitness.Value;
                    }
                    if (cProps.Skill.HasValue)
                    {
                        value = value + cProps.Skill.Value;
                    }
                    if (value < 0)
                    {
                        strHelp = "捣乱";
                    }
                    string strInfo = userName + "使用" + cProps.PName + "对他" + strHelp;
                    strUl = strUl.Replace("$Info$", strInfo);
                    string strTime = string.Empty;
                    DateTime happenTime = supporter.Time.Value;
                    DateTime now = DateTime.Now;
                    TimeSpan span = now.Subtract(happenTime);
                    strTime = span.Hours.ToString() + "小时" + span.Minutes.ToString() + "分钟前";
                    strUl = strUl.Replace("$Time$", strTime);
                    sbString.Append(strUl);
                }
            }
            else
            {
                string infoString = "比赛尚未开始";
                sbString.Append(infoString);
            }
            strRaceInfo = strRaceInfo.Replace("$Ul$", sbString.ToString());
            return strRaceInfo;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
